﻿Ext.ns('app.util');

app.util.handleResponse = function (response, context, callback) {

	var o = Ext.decode(response.responseText);

	if (o.success) {

		if (typeof callback == 'function') {
			callback(context);
		}

		if (o.msg != '') {
			Ext.Msg.alert(app.lang.Success, o.msg);
		}
	}
	else {

		if (o.msg != '') {
			Ext.Msg.alert(app.lang.Failure, o.msg);
		}
	}

};

// Needed to include it inside the JsonResponse object
Ext.override(Ext.tree.TreeLoader,
{
	processResponse: function (response, node, callback) {

		var json = response.responseText;

		try {

			var o = Ext.decode(json);

			if (o.success) { // Update node

				o = o.data; // The tree data

				node.beginUpdate();

				for (var i = 0, len = o.length; i < len; ++i) {
					
					var n = this.createNode(o[i]);

					if (n) {
						node.appendChild(n);
					}
				}

				node.endUpdate();

				if (typeof callback == 'function') {
					callback(this, node);
				}
			}
			else {
				Ext.MessageBox(o.msg);
			}
		}
		catch (e) {
			this.handleFailure(response);
		}
	}
});

Ext.override(Ext.data.JsonReader,
{
	read: function (response) {

		var json = response.responseText;

		var o = Ext.decode(json);

		if (o.success) {

			o = o.data;

			return this.readRecords(o);

		}
		else {
			Ext.Msg.alert(app.lang.Failure, o.msg);
			return o;
		}
	},

	getSuccess: function (o) {
		return o.success;
	}
});


Ext.override(Ext.form.BasicForm, {

    submit: function (options) {

        var _options = {

            success: function (form, action) {

                action.options.onSuccess(form, action);

                action.result = Ext.decode(action.response.responseText);

                if (action.result.msg != '') {
                    Ext.Msg.alert("Success", action.result.msg);
                }
            },

            failure: function (form, action) {

                if (form.errorReader) {

                    var rs = form.errorReader.read(action.response);
                    var errors = [];

                    if (rs.records) {
                        for (var i = 0, len = rs.records.length; i < len; ++i) {
                            var r = rs.records[i];
                            errors[i] = r.data;
                        }
                    }

                    if (errors.length < 1) {
                        errors = null;
                    }

                    action.result = {
                        success: rs.success,
                        errors: errors
                    };

                }
                else {
                    //                     switch (action.failureType) {
                    //                        case Ext.form.Action.CLIENT_INVALID: {
                    //                            Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                    //                            }
                    //                            break;
                    //                        case Ext.form.Action.CONNECT_FAILURE: {
                    //                            Ext.Msg.alert('Failure', 'Ajax communication failed');
                    //                            }
                    //                            break;
                    //                        case Ext.form.Action.SERVER_INVALID: {
                    //                           Ext.Msg.alert('Failure', action.result.msg);
                    //                           }
                    action.result = Ext.decode(action.response.responseText);

                    if (action.result.msg != '') {
                        Ext.Msg.alert("Failure", action.result.msg);
                    }
                }
            }
        };

        Ext.apply(_options, options);
        this.doAction('submit', _options);
        return this;
    }
});
